package com.fankui.servicevod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.fankui.commonutils.R;
import com.fankui.servicebase.exceptionhandler.LegendaryException;
import com.fankui.servicevod.service.VodService;
import com.fankui.servicevod.utils.ConstantPropertiesUtils;
import com.fankui.servicevod.utils.VodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName VodController
 * Description TODO
 * Date 2023/11/29 1:32
 *
 * @Author 范奎
 * @Since JDK 11
 */
@Api(description = "阿里云VOD")
@RestController
@RequestMapping("/vodservice")
public class VodController {
    @Autowired
    private VodService vodService;

    @ApiOperation(value = "上传视频")
    @PostMapping("upload")
    public R uploadVodVideo(@ApiParam(name = "file", value = "视频上传对象", required = true) MultipartFile file) {
        String videoId = vodService.uploadVideo(file);
        return R.ok().data("videoId", videoId);
    }

    @ApiOperation(value = "根据视频id删除视频")
    @DeleteMapping("removeVodVideo/{vodId}")
    public R removeVodVideo(@ApiParam(name = "vodId", value = "视频id", required = true) @PathVariable String vodId) {
        try {
            // 初始化对象
            DefaultAcsClient client = VodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 向request设置视频id
            request.setVideoIds(vodId);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LegendaryException(20001, "删除视频失败！");
        }
    }

    @ApiOperation(value = "根据视频id集合删除多个视频")
    @DeleteMapping("removeVodVideoList")
    public R removeVodVideoList(@ApiParam(name = "vodIdList", value = "视频id集合", required = true) @RequestParam("vodIdList") List<String> vodIdList) {
        try {
            // 初始化对象
            DefaultAcsClient client = VodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            // 创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 将vodIdList进行字符串拼接
            String vodIds = StringUtils.join(vodIdList.toArray(), ",");
            // 向request设置视频id
            request.setVideoIds(vodIds);
            // 调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new LegendaryException(20001, "删除视频失败！");
        }
    }

    @ApiOperation(value = "根据视频id获取视频凭证")
    @GetMapping("getPlayAuth/{videoId}")
    public R getPlayAuth(@ApiParam(name = "videoId", value = "视频id", required = true) @PathVariable String videoId) {
        try {
            // 创建初始化对象
            DefaultAcsClient client = VodClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            // 创建获取凭证request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            // 向request设置视频id
            request.setVideoId(videoId);
            // 调用方法得到凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();
            return R.ok().data("playAuth", playAuth);
        } catch (Exception e) {
            throw new LegendaryException(20001, "获取凭证失败！");
        }
    }
}
